const config = {
  debug: false,
};

const demoCardId = "py5F1wWko7pRFaXBMGJD1vK6PdjE";
console.log(location.href);
window
  .fetch(`/get-signature?url=${location.href}`, {
    credentials: "same-origin",
  })
  .then((res) => res.json())
  .then((data) => {
    config.appId = data.appId;
    config.nonceStr = data.nonceStr;
    config.signature = data.signature;
    config.timestamp = data.timestamp;
    config.jsApiList = [
      "hideMenuItems",
      "onMenuShareTimeline",
      // 'chooseCard',
      // 'addCard',
      // 'openCard',
      // 'chooseWXPay',
      "updateAppMessageShareData",
      "updateTimelineShareData",
    ];

    window.wechatObj = new window.WechatJSSDK(config);

    window.wechatObj
      .initialize()
      .then((w) => {
        //w is same as window.wechatObj
        const img = "img/bgm-switch.png";
        //sugar method
        window.wechatObj.shareOnMoment({
          title: "onMenuShareTimeline test title",
          type: "link",
          // link: 'http://btsousou.cn/'+ "{{pagename}}",
          success: function () {
            console.log("share on moment success");
          },
          cancel: function () {
            console.log("share on moment canceled");
          },
          imgUrl: img,
        });
        w.callWechatApi("onMenuShareAppMessage", {
          title: "onMenuShareAppMessage test title",
          type: "link",
          // link: 'http://btsousou.cn/'+ "{{pagename}}",
          desc: "onMenuShareAppMessage share description",
          success: function () {
            console.log("share on chat success");
          },
          cancel: function () {
            console.log("share on chat canceled");
          },
          imgUrl: img,
        });

        //v1.4.0 new api
        w.callWechatApi(
          "updateTimelineShareData",
          {
            title: "updateTimelineShareData test title",
            type: "link",
            // link: 'http://btsousou.cn/'+ "{{pagename}}",
            imgUrl: img,
          },
          (res) => {
            console.log("updateTimelineShareData callback: ", res);
          }
        );
        w.callWechatApi(
          "updateAppMessageShareData",
          {
            title: "测试网页的标题",
            type: "link",
            // link: 'http://btsousou.cn/'+ "{{pagename}}",
            desc: "测试网页的描述",
            imgUrl: img,
          },
          (res) => {
            console.log("updateAppMessageShareData callback: ", res);
          }
        );
      })
      .catch((err) => {
        console.error(err);
      });
  });
